package com.group01.web.controller;

import com.group01.result.Result;
import com.group01.service.ShopCarService;
import com.group01.vo.ShopCar.ShopCarGetInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import java.util.List;

/**
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/23
 */
@RestController
@RequestMapping("/shopCar")
@Validated
@Api(tags = "购物车模块")
public class ShopCarController {

    /**
     * 注入
     */
    private final ShopCarService shopCarService;

    public ShopCarController(ShopCarService shopCarService) {
        this.shopCarService = shopCarService;
    }

    /**
     * 查询当前用户的购物车列表
     * @return {@link Result }<{@link List }<{@link ShopCarGetInfoVO }>>
     */
    @ApiOperation("查询当前用户的购物车列表")
    @GetMapping
    public Result<List<ShopCarGetInfoVO>> getShopCarInfoList() {
        return shopCarService.getShopCarInfoList();
    }

    /**
     * 获取购物车数量
     * @return {@link Result }<{@link Integer }>
     */
    @GetMapping("/cartList")
    public Result<Long> getCartSize() {
        return shopCarService.getCartSize();
    }

    /**
     * 添加购物车
     * @param id 商品id
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("添加购物车")
    @PostMapping("/{id}/{num}")
    public Result<String> AddShopCarInfo(
            @PathVariable @DecimalMin(value = "0" , message = "id是数字且大于0") Integer id,
            @PathVariable @DecimalMin(value = "0" , message = "num是数字且大于0") Integer num
    ) {
        return shopCarService.AddShopCarInfo(id, num);
    }

    /**
     * 修改购物车数量根据购物车主键
     * @param id 购物车主键
     * @param num 商品加购数量
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改购物车数量根据购物车主键")
    @PutMapping("/{id}/{num}")
    public Result<String> updateShopCarNums(
            @PathVariable @DecimalMin(value = "0" , message = "id是数字且大于0") Integer id,
            @PathVariable @DecimalMin(value = "0" , message = "num是数字且大于0") Integer num
    ) {
        return shopCarService.updateShopCarNums(id, num);
    }

    /**
     * 删除与批量删除购物车信息
     * @param ids 购物车主键集合
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("删除与批量删除购物车信息")
    @DeleteMapping
    public Result<String> deleteShopCarInfo(
            @RequestBody List<Integer> ids
    ) {
        return shopCarService.deleteShopCarInfos(ids);
    }
}
